package leetcode.string;

/**
 * @author mazouri
 * @create 2021-09-30 20:02
 */
public class _6_convert {
    public String convert(String s, int numRows) {
        if (numRows == 1) return s;

        char[][] chars = new char[numRows][1000];
        StringBuilder sb = new StringBuilder();
        int i = 0;
        int j = 0;
        boolean flag = true;

        if (numRows == 2) {
            for (int m = 0; m < s.length(); m+=2) {
                sb.append(s.charAt(m));
            }
            for (int m = 1; m < s.length(); m+=2) {
                sb.append(s.charAt(m));
            }
            return sb.toString();
        }

        for (int k = 0; k < s.length(); k++) {
            if (flag){
                chars[i][j] = s.charAt(k);
                if (i == numRows-1) {
                    flag = false;
                    i--;
                    j++;
                    continue;
                }

                i++;
            }else {
                chars[i][j] = s.charAt(k);
                i--;
                j++;

                if (i==0) flag = true;
            }
        }

        for (char[] aChar : chars) {
            for (char c : aChar) {
                if (c != '\u0000'){
                    sb.append(c);
                }
            }
        }

        return sb.toString();
    }

    public static void main(String[] args) {
        System.out.println(new _6_convert().convert("ABCDEF", 2));
    }
}
